import 'package:flutter/material.dart';
import '../services/auth_service.dart';
import '../widgets/preview/theme_config_panel.dart';
import 'login_page.dart';
import 'server_config_page.dart';

class ProfilePage extends StatelessWidget {
  const ProfilePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('我的'),
        backgroundColor: Theme.of(context).primaryColor,
      ),
      body: ListView(
        children: [
          // 主题设置
          ListTile(
            leading: Icon(
              Icons.color_lens,
              color: Theme.of(context).primaryColor,
            ),
            title: const Text('主题设置'),
            subtitle: const Text('自定义应用主题颜色和背景'),
            onTap: () {
              showThemeConfigPanel(context);
            },
          ),
          const Divider(),

          ListTile(
            leading: const Icon(Icons.settings),
            title: const Text('服务器配置'),
            onTap: () {
              Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (context) => const ServerConfigPage(),
                ),
              );
            },
          ),
          ListTile(
            leading: const Icon(Icons.logout, color: Colors.red),
            title: const Text('退出登录'),
            onTap: () async {
              final confirmed = await showDialog<bool>(
                context: context,
                builder:
                    (context) => AlertDialog(
                      title: const Text('退出确认'),
                      content: const Text('确定要退出登录吗？'),
                      actions: [
                        TextButton(
                          onPressed: () => Navigator.of(context).pop(false),
                          child: const Text('取消'),
                        ),
                        TextButton(
                          onPressed: () => Navigator.of(context).pop(true),
                          style: TextButton.styleFrom(
                            foregroundColor: Colors.red,
                          ),
                          child: const Text('退出'),
                        ),
                      ],
                    ),
              );

              if (confirmed == true && context.mounted) {
                await AuthService.logout();
                if (context.mounted) {
                  Navigator.of(context).pushAndRemoveUntil(
                    MaterialPageRoute(builder: (context) => const LoginPage()),
                    (route) => false,
                  );
                }
              }
            },
          ),
        ],
      ),
    );
  }
}
